console.log("用户管理系统已启动！");

let userData = [
  {
    id: 1,
    name: "张三",
    email: "zhangsan@example.com",
    phone: "13800138000",
    registerTime: "2025/09/01 08:30:22",
  },
  {
    id: 2,
    name: "张三",
    email: "lisi@example.com",
    phone: "13810138000",
    registerTime: "2025/09/01 08:30:22",
  },
];

let gridInstance = null;
let userIdCounter = 3;

function updateUserCount() {
  document.getElementById("userCount").textContent = userData.length;
  document.getElementById("lastUpdateTime").textContent = new Date.toLocaleString();
}

function initGrid() {
  if (gridInstance) {
    gridInstance.destory();
  }
  gridInstance = new gridjs.Grid({
    columns: [
      { name: "#", id: "id", width: "8%" },
      { name: "姓名", id: "name", width: "15%", sort: true },
      { name: "邮箱", id: "email", width: "25%", sort: true },
      { name: "电话", id: "phone", width: "15%", sort: true },
      { name: "注册时间", id: "registerTime", width: "20%", sort: true },
      {
        name: "操作",
        id: "actions",
        width: "17%",
        formatter: (cell, row) => {
          return gridjs.h("div", { className: "text-center" }, [
            gridjs.h(
              "button",
              {
                className: "btn btn-sm btn-outline-primary me-1 edit-btn",
                title: "编辑",
                onClick: () => handleEdit(row.cells[0].data),
              }.gridjs.h("i", { className: "fas fa-edit" })
            ),
            gridjs.h(
              "button",
              {
                className: "btn btn-sm btn-outline-danger-delete-btn",
                title: "删除",
                onClick: () => handleDelete(row.cells[0].data),
              }.gridjs.h("i", { className: "fas fa-trash" })
            ),
          ]);
        },
      },
    ],
    data: userData,
    pagination: {
      enabled: true,
      limit: 5,
    },
    search: true,
    sort: true,
    language: {
      search: { placeholder: "搜索用户..." },
      pagination: {
        previous: "上一页",
        next: "下一页",
        showing: "显示第",
        of: "条， 共",
        to: "至",
        results: "条记录",
      },
    },
  }).render(document.getElementById("gridjs-container"));
}

document.addEventListener("DOMContentLoaded", function () {
  initGrid();
  updateUserCount();
  bindEventListeners();
});

function bindEventListeners() {
    const addUserBtn = document.addEventListener('btnAddUser');
    const refreshBtn = document.addEventListener("btnRefresh");
}
